﻿using MediatR;
using Microsoft.AspNetCore.Mvc;
using Production.ApplicationLib.Command;
using Production.ApplicationLib.Queries;
using Production.CommonLib.Constants;
using Production.CommonLib.Models;

namespace Production.WebApi.Controllers
{
    /// <summary>
    /// 工序
    /// </summary>
    [ApiController]
    public class ProcessController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly IProcessQueries _processQueries;

        public ProcessController(IMediator mediator,
            IProcessQueries processQueries)
        {
            _mediator = mediator;
            _processQueries = processQueries;
        }

        [HttpGet(ApiRoutes.Process.QueryProcesses)]
        public async Task<IActionResult> Get([FromQuery] ProcessSearchRequestDto model)
        {
            if (model.Page > 0 || model.Limit > 0)
            {
                var data = await _processQueries.QueryProcessesByPage(model);
                return Ok(data);
            }
            else
            {
                var data = await _processQueries.QueryProcesses(model);
                return Ok(data);
            }
        }

        [HttpPost(ApiRoutes.Process.CreateProcess)]
        public async Task<IActionResult> Post(CreateProcessCommand command)
        {
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        [HttpGet(ApiRoutes.Process.GetProcess)]
        public async Task<IActionResult> Get(int id)
        {
            var result = await _processQueries.GetProcess(id);
            return Ok(result);
        }

        [HttpPut(ApiRoutes.Process.UpdateProcess)]
        public async Task<IActionResult> Put(int id, UpdateProcessCommand command)
        {
            command.Id = id;
            var result = await _mediator.Send(command);
            return Ok(result);
        }
    }
}